import asyncio
import time
from functools import partial

import aiohttp
from lxml import etree

start = time.time()

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61"
                  " Safari/537.36"
}
urls = ['http://www.chamang.cn/article/1/']
base_url = 'http://www.chamang.cn/article/%d/'


async def get_list_page(req_url):
    # async定义一个协程函数
    async with aiohttp.ClientSession() as session:
        async with await session.get(req_url, headers=headers) as response:
            print("请求url:%s,响应码是：%d" % (req_url, response.status))
            if response.status == 200:
                html = await response.text()
                selector = etree.HTML(html)
                href_path = "//div[@class='list']/ul/li[@class='img']/a/@href"
                next_page_path = "//div[@id='pages']/a[@class='nextpage']/@href"
                # 文章详情页url
                href_list = selector.xpath(href_path)
                # 下一页的url
                # next_page_url = selector.xpath(next_page_path)[0]
                # print(href_list, next_page_url)
                return href_list


def callback(future):
    detail_href_list = future.result()
    print(detail_href_list)


tasks = []

for url in urls:
    # 协程函数返回的是协程对象，函数内部代码是不会执行的
    c = get_list_page(url)
    task = asyncio.ensure_future(c)
    # print(task.result())
    task.add_done_callback(callback)
    tasks.append(task)

# 生成一个事件循环器
loop = asyncio.get_event_loop()
# 将任务放到任务列表，等待任务循环执行
loop.run_until_complete(asyncio.wait(tasks))

end = time.time()

print("总耗时：", end - start)
